local houduan = fk.CreateSkill{
  name = "wk_heg__houduan",
}

local HG = require "packages/new_heg_diy_my/util"
local H = require "packages/ol_hegemony/util"
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["wk_heg__houduan"] = "后断",
  [":wk_heg__houduan"] = "与你势力相同角色使用牌结算后，你可以弃置一张花色相同的明置牌，对此牌造成过伤害的一名角色造成1点伤害",
  ["#wk_heg__houduan_prompt"] = "后断：你可以弃置一张%arg明置牌，对此牌造成过伤害的一名角色造成1点伤害",
}

houduan:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and H.compareKingdomWith(player, target)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local able_choose = {}
    for _, p in pairs(data.tos) do 
      if #room.logic:getActualDamageEvents(1, function (e) return e.data.to == p and e.data.card ~= nil and e.data.card == data.card end) > 0 then
        table.insert(able_choose, p)
      end
    end
    if #able_choose == 0 then return end
    local ids = table.concat(
      table.filter(HG.getShownCards(player), function (cid) 
        return data.card.suit == Fk:getCardById(cid).suit and not player:prohibitDiscard(cid)
      end), ",")
    if #ids == 0 then return end
    local p, card = room:askToChooseCardsAndPlayers(player,{ 
      targets = able_choose,
      min_num = 1, max_num = 1,
      min_card_num = 1, max_card_num = 1,
      pattern = ".|.|.|.|.|.|"..ids, 
      prompt = "#wk_heg__houduan_prompt:::"..U.ConvertSuit(data.card.suit, "int", "sym"),
      skill_name = houduan.name,
      cancelable = true})
    if card then
      event:setCostData(self, {card = card, to = p[1]})
      return true 
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    room:throwCard(cost_data.card, houduan.name, player, player)
    room:damage{
      from = player,
      to = cost_data.to,
      damage = 1,
      skillName = houduan.name
    }
  end
})

return houduan